
import java.util.*;
import java.util.regex.Pattern;
/**
 * Starter class used to start any of the tasks
 * @author skinner@kth.se
 * @author aidi@kth.se
 */
class Start {
	// Default start value for crawling and polling network
	public final static String DEFAULT_IP		= "192.168.1.10";
	public final static int DEFAULT_INTERVAL	= 1000 * 2;
	public final static int DEFAULT_TIMESPAN	= 1000 * 60 * 3;
	public final static int DEFAULT_K			= 4;
	public final static int DEFAULT_REPEATS		= 3;
	public final static int DEFAULT_W 			= 30;
	public final static int MAX_CLUSTERING_ITERATIONS = 10;
	public final static boolean USE_OUTPUT_FILE = false;
	
	public static void main(String args[]){
		Date start = new Date();
		
		/*
		 * Running TASK 1
		   java Start -t 1 -ip <startIp> 
		 * Running TASK 2
		   java Start -t 2 -ip <startIp> -i <interval> -ts <timespan> -k <k_mean_value> -r <repeats>
		 * Running TASK 3
		   java Start -t 3 -ip <startIp> -w <polling_rounds>  -r <repeats>
		 *   
		 * Make Parameter -t task_number parameter as mandatory, to identify what operation has to be done 
		 * Make Parameters inside <> as optional, the order of these doesn't matter  
		 */
		
		int task = 0;
		
		// Get optional parameters from command line, if available, otherwise use default
		String startIp = DEFAULT_IP;
		int interval = DEFAULT_INTERVAL;
		int timespan = DEFAULT_TIMESPAN;
		int k = DEFAULT_K;
		int repeats = DEFAULT_REPEATS;
		int w = DEFAULT_W;
		boolean useOutputFile = USE_OUTPUT_FILE;
		
		int i = 0;
		if(args.length == 0){
			System.out.println("Please enter arguments in one of the following formats");
			System.out.println("\tjava Start -t 1 -ip <startIp>");
			System.out.println("\tjava Start -t 2 -ip <startIp> -i <interval> -ts <timespan> -k <k_mean_value> -r <repeats>");
			System.out.println("\tjava Start -t 3 -ip <startIp> -w <polling_rounds>  -r <repeats>");
			System.exit(-1);	
		}else{	
	        while (i < args.length) {
	            if (i == 0 && args[i].equals("-t")) {
	            	try {
	            		task = Integer.parseInt(args[i+1]);
					} catch (Exception e) {
						System.err.println("ERROR: Invalid Task.  Please re-enter an integer (1-3).");
						System.exit(-1);
					}
	                i++;
	            } else if (args[i].equals("-ip")) {
	            	try {
		            	if (args.length > i && Pattern.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}", args[i+1])) {
		    				startIp = args[i+1];
		    			} else {
		    				System.err.println("ERROR: Invalid IP Address supplied. Using default: " + DEFAULT_IP);
		    			}
		    		} catch(Exception e) {
		    			System.err.println("ERROR: Invalid IP Address supplied. Using default: " + DEFAULT_IP);
		    		}
	                i++;
	            } else if (args[i].equals("-i")) {
	            	try {
						if(args.length > i && Integer.parseInt(args[i+1]) > 0 ){
							interval = Integer.parseInt(args[i+1]) * 1000;
						} else {
							System.err.println("ERROR: Invalid interval supplied.  Using default: " + DEFAULT_INTERVAL/1000);
		    			}
					} catch (Exception e) {
						System.err.println("ERROR: Invalid interval supplied.  Using default: " + DEFAULT_INTERVAL/1000);
					}
	                i++;
	            } else if (args[i].equals("-k")) {
	            	try {
	            		if(args.length > i && Integer.parseInt(args[i+1]) > 0 ){
	            			k = Integer.parseInt(args[i+1]);
						} else {
							System.err.println("ERROR: Invalid K-value supplied.  Using default: " + DEFAULT_K);
		    			}
					} catch (Exception e) {
						System.err.println("ERROR: Invalid K-value supplied.  Using default: " + DEFAULT_K);
					}
	                i++;
	            } else if (args[i].equals("-ts")) {
	            	try {
	            		if(args.length > i && Integer.parseInt(args[i+1]) > 0){
	            			timespan = Integer.parseInt(args[i+1]) * 1000;
						} else {
							System.err.println("ERROR: Invalid timespan supplied.  Using default: " + DEFAULT_TIMESPAN/1000);
		    			}
					} catch (Exception e) {
						System.err.println("ERROR: Invalid timespan supplied.  Using default: " + DEFAULT_TIMESPAN/1000);
					}
	                i++;
	            } else if (args.length > i && args[i].equals("-r")) {
	            	try {
	            		if( Integer.parseInt(args[i+1]) > 0 ){
	            			repeats = Integer.parseInt(args[i+1]);
						} else {
							System.err.println("ERROR: Invalid repetition supplied.  Using default: " + DEFAULT_REPEATS);
		    			}
					} catch (Exception e) {
						System.err.println("ERROR: Invalid repetition supplied.  Using default: " + DEFAULT_REPEATS);
					}
	                i++;
	            } else if(args.length > i && args[i].equals("-w") && task == 3){
	            	try {
	            		if(Integer.parseInt(args[i+1]) > 30 && Integer.parseInt(args[i+1]) < 60){
	            			w = Integer.parseInt(args[i+1]);
						} else {
							System.err.println("ERROR: Invalid w supplied. Required: 30 <= w <= 60. Using default: " + DEFAULT_REPEATS);
		    			}
					} catch (Exception e) {
						System.err.println("ERROR: Invalid w supplied.  Using default: " + DEFAULT_REPEATS);
					}
	                i++;
	            } else if(args.length > i && args[i].equals("-o") ){
	            	try {
	            		if( Integer.parseInt(args[i+1]) == 1 ){
	            			useOutputFile = true;
						} else {
							System.err.println("ERROR: Invalid output supplied (1 or 0).  Using default: " + USE_OUTPUT_FILE);
		    			}
					} catch (Exception e) {
						System.err.println("ERROR: Invalid output supplied (1 or 0).  Using default: " + USE_OUTPUT_FILE);
					}
	                i++;
	            }
	            i++;
	        }
		}
        // Message to user about executed task 
		System.out.println("Starting EP2300 SNMP assignment, Task " 
				+ task + " ("
				+ (task == 1 ? "Discovering the Network Topology" 
				: (task == 2 ? "Clustering Global States" 
				: "Online Anomaly Detection System"))
				+ ")");
		// Messages to user who started application about using defaults
		System.out.println("Beginning crawl at IP: " + startIp + (startIp == DEFAULT_IP ? " (default)" : ""));
		SNMPCrawler sc = new SNMPCrawler(startIp);
		System.out.println("Crawling completed: " + ((int) (System.currentTimeMillis() - start.getTime())/1000) + "s\n");

		if(task == 2){
			System.out.println("Beginning polling...");
			System.out.println("Using interval			:\t" + (interval/1000) + (interval == DEFAULT_INTERVAL ? " seconds (default)" : "s"));
			System.out.println("Using timespan			:\t" + (timespan/1000) + (timespan == DEFAULT_TIMESPAN ? " seconds (default)" : "s"));
			System.out.println("Using K-value			:\t" + k + (k == DEFAULT_K ? " (default)" : ""));
			System.out.println("Will repeat calculations 	:\t" + repeats + (repeats == DEFAULT_REPEATS ? " times (default)\n" : " times\n"));
			
			SNMPPoller.poll(sc.getRouters(), interval, timespan, k, repeats, useOutputFile);
			//System.out.println("\nTime: " + ((int) (System.currentTimeMillis() - start.getTime())/1000) + "s");
			
		}else if(task == 3){
			System.out.println("Beginning Online polling...");
			System.out.println("Using w				:\t" + w + (w == DEFAULT_W ? " polling_rounds (default)" : ""));
			System.out.println("Using K-value			:\t" + k + (k == DEFAULT_K ? " (default)" : ""));
			System.out.println("Will repeat calculations 	:\t" + repeats + (repeats == DEFAULT_REPEATS ? " times (default)\n" : " times\n"));
			
			SNMPPoller.onlinePoll(sc.getRouters(), interval, w, k, repeats, useOutputFile);
		}
	}
	

}
